package com.copm.ifm.common.process.entity.excute;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.copm.ifm.base.api.pojo.BasePO;
import com.copm.ifm.base.basic.util.AuthUtils;
import com.copm.ifm.common.process.core.emum.ProcessExecuteStatusEnum;
import com.copm.ifm.common.process.entity.template.bo.ProcessDetailBO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 流程运行表
 *
 * @author zzf
 * @since 2019-04-11
 */

@Data
@EqualsAndHashCode(callSuper = true)
@TableName("wf_process_execute")
@ApiModel("流程运行实体对象")
public class ProcessExecute extends BasePO {

    @ApiModelProperty(value = "主键ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty("流程id")
    private Long processId;

    @ApiModelProperty("发起人id")
    private Long executeUserId;

    @ApiModelProperty("流程定义编码")
    private String processDefineCode;

    @ApiModelProperty("对象数据id")
    private Long targetId;

    @ApiModelProperty("此次流程走的节点id集合")
    private List<Long> nodeIdList;

    @ApiModelProperty(value = "当前运行的节点", notes = " 用二进制数字表示 如：10000 表示总共5个节点，目前在第一个节点")
    private String executeInfo;

    /**
     * @see com.copm.ifm.common.process.core.emum.ProcessExecuteStatusEnum
     */
    @ApiModelProperty("流程状态 0: 初始化中 1：流程中 -2：已驳回 2：已完成 -1：已撤回")
    private Integer runStatus;


    public void addExecuteNodeId(Long id) {
        if (this.nodeIdList == null) {
            this.nodeIdList = new ArrayList<>();
        }
        this.nodeIdList.add(id);
    }

    public static ProcessExecute init(ProcessDetailBO process, Serializable id) {
        ProcessExecute processExecute = new ProcessExecute();
        processExecute.setProcessId(process.getId());
        processExecute.setExecuteUserId(AuthUtils.getCurrentUserId());
        processExecute.setProcessDefineCode(process.getProcessDefineCode());
        processExecute.setTargetId(Long.valueOf(id.toString()));
        processExecute.setNodeIdList(ListUtil.empty());
        processExecute.setExecuteInfo("0");
        processExecute.setRunStatus(ProcessExecuteStatusEnum.INIT.getStatus());
        return processExecute;
    }

}
